<template>
  <div class="content">
    <nut-navbar class="navbar" z-index="5" :clickable="false" fixed safe-area-inset-top>
      <template #content>
        <span>{{ t(String(route.meta.title)) }}</span>
      </template>
      <template #left>
        <img src="/imgs/club/arrow_left.png" alt="" @click="router.go(-1)" class="arrow_left" />
      </template>
      <template #right>
        <span @click="onSubmitCreate" class="save">保存</span>
      </template>
    </nut-navbar>

    <div class="top_upload">
      <nut-uploader
          accept=".jpg,.jpeg,.png,.gif,.webp"
          :before-xhr-upload="beforeUpload"
          @delete="(e) => {removeImg(e)}"
      >
      </nut-uploader>
      <p>{{t('上传俱乐部图片')}}</p>
      <p>限JPG,JPEG,PNG格式,10MB以内</p>
    </div>
    <div class="form_box">
      <nut-form label-position="top" ref="formRef" >
        <nut-form-item>
          <nut-input v-model.trim="createParams.name" :placeholder="t('请输入俱乐部名称')" clearable />
        </nut-form-item>
        <nut-form-item>
          <nut-textarea :rows="3" v-model.trim="createParams.dec" :placeholder="t('请输入俱乐部简介')" clearable />
        </nut-form-item>
      </nut-form>
    </div>

    <div class="list_box">
      <div class="other_list_item mb-10" v-for="item in buttons" :key="item.id"
          @click="goToPage(item)">
          <div class="item_left">
            <ImgT :src="item.icon" alt="" />
            <span>{{ t(item.text) }}</span>
          </div>
          <div class="item_right">
            <nut-switch v-if="item.value == 'check'" v-model="isCheckSwitch" active-color="#1DBA8B" inactive-color="#504F6F"></nut-switch>
            <nut-switch v-else-if="item.value == 'manager'" v-model="isManagerSwitch" active-color="#1DBA8B" inactive-color="#504F6F"></nut-switch>
            <ImgT src="/imgs/club/arrow.png" alt="" v-else />
          </div>
        </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useI18n } from "vue-i18n";
import { ref } from "vue";
import {useRoute, useRouter} from "vue-router";
import {showNotify} from "@nutui/nutui";
import {storeToRefs} from "pinia";
import {User} from "@/store/user.ts";
import pinia from "@/store";

const route = useRoute();
const router = useRouter();
const { t } = useI18n();
const UserStore = User(pinia);
const { roleInfo } = storeToRefs(UserStore);
// 创建俱乐部表单提交
const createParams = ref({
  name: '',
  dec: '',
});

const isCheckSwitch = ref(false);
const isManagerSwitch = ref(false);

const buttons:any = ref([
  {
    id: 1,
    text: '俱乐部成员免审核',
    value: 'check',
    icon: '/imgs/club/check.png',
    route: '/club/create',
  },
  {
    id: 2,
    text: '管理员创建牌局',
    value: 'manager',
    icon: '/imgs/club/manager.png',
    route: '',
  },
  {
    id: 3,
    text: '成员管理',
    value: '',
    icon: '/imgs/club/member.png',
    route: '/club/member',
  },
  {
    id: 4,
    text: '牌局管理',
    value: '',
    icon: '/imgs/club/gambling.png',
    route: '/club/handList',
  },
  {
    id: 5,
    text: '解散俱乐部',
    value: '',
    icon: '/imgs/club/disband.png',
    route: '',
  },
]);

// 上传图片
const beforeUpload = (xhr: any, data: any) => {
  console.log(xhr)
  const file = data.sourceFile

  if (file && file.size > 1024 * 1024 * 2) { // 2MB限制
    showNotify.danger('文件大小不能超过2MB！')
    return;
  }
  const formData = new FormData();
  formData.append('avatar', file);
  formData.append('role_id', `${roleInfo.value.id}`);
  fetch(`http://18.162.112.52:8031/api/upload/avatar`, {
    method: 'POST',
    body: formData,
  }).then(response => response.json()).then(response => {
    if (response.status == 200 || response.status == 'success') {
      const urlImg = response.data.path
      showNotify.success(response.message)
      console.log('&&&&&', urlImg)
    } else {
      showNotify.danger(response.message)
    }
  })
}
const removeImg = (e: any) => {
  console.log('删除---', e)
}
// 创建俱乐部提交
const onSubmitCreate = () => {
  console.log('----------', createParams);
};

const goToPage = (item: any) => {
  if (item.route) {
    router.push(`${item.route}`)
  }
}

</script>

<style scoped lang="scss">
.content {
  font-size: 14px;
  margin: 0 0 10px;

  .top_upload {
    padding: 15% 0 0;
    p {
      font-size: 15px;
      color: rgba(192, 194, 219, 1);
      text-align: center;
      margin-top: 10px;
    }
    :deep(.nut-uploader) {
      margin: 0 auto;
      width: 120px;
      height: 120px;
      background: rgba(26, 12, 66, 1);
      border: 1px solid rgba(50, 44, 89, 1);
      border-radius: 50%;
      display: flex;
      justify-content: center;
      align-items: center;

      .nut-uploader__upload {
        background: url(/imgs/club/add_ren.png) center no-repeat;
        background-size: 70px;
        border-radius: 50%;

        &::before {
          content: '';
          position: absolute;
          width: 32px;
          height: 32px;
          border-radius: 50%;
          bottom: 0;
          right: 6px;
          background: radial-gradient(100% 103.23% at 50% 0%, #1170FF 0%, #1154FF 61.51%, #6B11FF 98.5%);
        }
        svg {
          width: 24px;
          height: 24px;
          color: #fff !important;
          position: absolute;
          bottom: 4px;
          right: 10px;
        }
      }
      .nut-uploader__upload, .nut-uploader__preview, .nut-uploader__preview-img {
        width: 100%;
        height: 100%;
        margin-right: 0;
        .close {
          color: #fff;
        }
      }
      .nut-uploader__preview__progress {
        display: none;
      }
    }
  }
  .form_box {
    width: 347px;
    height: 176px;
    margin: 10px auto 0;
    padding: 10px;
    border-radius: 8px;
    background: #100E26;
    box-shadow: 0.5px 0.5px 1px 0px #2E296B inset;
    form {
      .add_title {
        font-size: 14px;
        color: #fff;
        margin-bottom: 10px;
      }

      :deep(.nut-cell-group__wrap) {
        box-shadow: none;
        background: none;
        margin: 0;

        .nut-cell {
          background: none;
          padding: 0;
          margin-bottom: 14px;

          &::after {
            border: none;
          }

          &.nut-form-item {

            .nut-form-item__label {
              color: #fff;
            }
            .nut-input,
            .nut-textarea {
              color: #fff;
              width: 100%;
              background: #1D1C3C;
              border: 1px solid #1D1C3C;
              border-radius: 8px;
              padding: 10px !important;
              .nut-textarea__textarea {
                color: #fff;
              }
            }

          }
        }
      }

    }
  }

  .arrow_left {
    width: 24px;
    height: 24px;
  }
  .save {
    width: 56px;
    height: 32px;
    line-height: 32px;
    text-align: center;
    color: #FFFFFF;
    background: linear-gradient(180deg, #5567FF 0%, #9E1EFF 100%);
    box-shadow: 0.5px 0.5px 1px 0px #9B9EFF inset;
    border-radius: 8px;
  }
  .list_box {
    width: 347px;
    margin: 0 auto;

    .other_list_item {
      height: 48px;
      border-radius: 12px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      // padding: 0 14px;

      .item_left {
          display: flex;
          align-items: center;

          img {
            width: 20px;
            height: 20px;
            margin-right: 8px;
          }

          span {
            color: #FFFFFF;
            font-weight: 500;
            font-size: 13px;
          }
      }

      .item_right {
        img {
          width: 14px;
          height: 14px;
        }
      }
      .arrow {
          height: 16px
      }
    }
  }
  
}
</style>
